package emulator.unittest.hardware.nmos6502.timing;

import emulator.EmulatorException;
import emulator.hardware.HwByte;
import emulator.hardware.memory.MemoryBlock;
import emulator.hardware.nmos6502.Cpu6502;
import emulator.hardware.nmos6502.Flags6502;
import emulator.hardware.video.test.CountingClock;

public class TimingTestBase {
	private static final int MEMORY_SIZE = 512;
	
	private Cpu6502 cpu;
	private CountingClock clock;
	private MemoryBlock memory_block;
	
	public TimingTestBase()
	{
		cpu = new Cpu6502();
		clock = new CountingClock();
		memory_block = new MemoryBlock(MEMORY_SIZE);
		cpu.attach(memory_block, clock);
	}
	
	public void resizeMemory(int size)
	{
		memory_block = new MemoryBlock(size);
		cpu.attach(memory_block, clock);
	}
	
	public void setMemoryWord(int address, int word) throws EmulatorException
	{
		memory_block.write(address, word & 0xFF);
		memory_block.write(address+1, (word >> 8) & 0xFF);
	}
	
	public void setRegisters(int accu, int x, int y, int flags)
	{
		cpu.setAccu(new HwByte(accu));
		cpu.setX(new HwByte(x));
		cpu.setY(new HwByte(y));
		cpu.setFlags(new Flags6502(flags));
	}
	
	public void setCommand(int address, int command, int op1, int op2) throws EmulatorException
	{
		memory_block.write(address, command);
		memory_block.write(address+1, op1);
		memory_block.write(address+2, op2);
		cpu.setPC(address);
	}
	
	public void execute() throws EmulatorException
	{
		cpu.setSP(new HwByte(0x7f));
		cpu.next();
	}
	
	public int getElapsedCycles()
	{
		return clock.getCounterValue();
	}
}
